unit storehouses;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DbCtrls,
  DBGrids, StdCtrls, ExtCtrls, PairSplitter, Grids, Buttons, ComCtrls;

type

  { TfrmStoreHouses }

  TfrmStoreHouses = class(TForm)
    btnDelete: TBitBtn;
    btnAdd: TBitBtn;
    btnCancel: TBitBtn;
    btnSave: TBitBtn;
    DBGStoreHouses: TDBGrid;
    DBNavigator: TDBNavigator;
    PairSplitter1: TPairSplitter;
    PairSplitterSide1: TPairSplitterSide;
    PairSplitterSide2: TPairSplitterSide;
    StatusBar: TStatusBar;
    StringGridGoods: TStringGrid;
    procedure btnAddClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure DBGStoreHousesCellClick(Column: TColumn);
    procedure DBNavigatorBeforeAction(Sender: TObject; Button: TDBNavButtonType
      );
    procedure FormShow(Sender: TObject);
    procedure refreshListOfGoods;
    procedure checkErrors;
    procedure StringGridGoodsDblClick(Sender: TObject);
    procedure StringGridGoodsDrawCell(Sender: TObject; aCol, aRow: Integer;
      aRect: TRect; aState: TGridDrawState);
    procedure StringGridGoodsKeyPress(Sender: TObject; var Key: char);
    procedure StringGridGoodsSelectCell(Sender: TObject; aCol, aRow: Integer;
      var CanSelect: Boolean);
    procedure StringGridGoodsValidateEntry(sender: TObject; aCol,
      aRow: Integer; const OldValue: string; var NewValue: String);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  frmStoreHouses: TfrmStoreHouses;
  isEdit:Boolean=False;
  isAppend:Boolean=False;
implementation

{$R *.lfm}

{ TfrmStoreHouses }

{ TODO 5 : Переміщення зі складу на склад }

uses main,nomenclatureselect;

procedure TfrmStoreHouses.checkErrors;
begin
 if isAppend then begin
   btnAdd.Enabled:=False;
   btnDelete.Enabled:=False;
   btnSave.Enabled:=True;
   btnCancel.Enabled:=True;
 end else begin
   btnAdd.Enabled:=True;
   btnDelete.Enabled:=True;
   btnSave.Enabled:=False;
   btnCancel.Enabled:=False;
 end;
 /////////////////////////////////////

 if isEdit then begin
   btnAdd.Enabled:=False;
   btnDelete.Enabled:=False;
   btnSave.Enabled:=True;
   btnCancel.Enabled:=True;
 end else begin
   btnAdd.Enabled:=True;
   btnDelete.Enabled:=True;
   btnSave.Enabled:=False;
   btnCancel.Enabled:=False;
 end;
 if (StringGridGoods.RowCount=1) then begin
   btnDelete.Enabled:=False;
 end;
end;

procedure TfrmStoreHouses.StringGridGoodsDblClick(Sender: TObject);
begin
  if (StringGridGoods.SelectedColumn.Index = 1) AND isAppend then begin
    FrmMain.clearSelect;
    frmNomenclatureSelect.ShowModal;
    StringGridGoods.Cells[2,StringGridGoods.Row]:=FrmMain.nomenclatureSelectedTitle;
    StringGridGoods.Cells[7,StringGridGoods.Row]:=IntToStr(FrmMain.nomenclatureSelectedId);
    //isEdit:=True;
    checkErrors();
  end;
end;

procedure TfrmStoreHouses.StringGridGoodsDrawCell(Sender: TObject; aCol,
  aRow: Integer; aRect: TRect; aState: TGridDrawState);
//var
  //dx: Integer;
begin
  {with (Sender as TStringGrid) do
  begin
    // Don't change color for first Column, first row
    if (ACol = 0) or (ARow = 0) then
      Canvas.Brush.Color := clBtnFace
    else
    begin
      case ACol of
        1: Canvas.Font.Color := clBlack;
        2: Canvas.Font.Color := clBlue;
      end;
      // Draw the Band
      if ARow mod 2 = 0 then
        Canvas.Brush.Color := $00E1FFF9
      else
        Canvas.Brush.Color := $00FFEBDF;
      Canvas.TextRect(aRect, aRect.Left + 2, aRect.Top + 2, cells[acol, arow]);
      Canvas.FrameRect(aRect);
    end;
  end; }
  //FrmMain.FillCell(StringGridGoods,3,1,clRed,clBlack);
end;

procedure TfrmStoreHouses.StringGridGoodsKeyPress(Sender: TObject; var Key: char
  );
begin
  FrmMain.KeyVerifyFloat(Sender,Key);
end;

procedure TfrmStoreHouses.StringGridGoodsSelectCell(Sender: TObject; aCol,
  aRow: Integer; var CanSelect: Boolean);
begin
  if isEdit then begin
    if StringGridGoods.Row<>aRow then begin
    CanSelect:=False;
    end else begin
    CanSelect:=True;
    end;
  end;
end;

procedure TfrmStoreHouses.StringGridGoodsValidateEntry(sender: TObject; aCol,
  aRow: Integer; const OldValue: string; var NewValue: String);
begin
  if ((aCol=3) AND (StringGridGoods.Cells[1,aRow]<>EmptyStr) AND (StringGridGoods.Cells[8,aRow]=EmptyStr) AND (OldValue<>NewValue)) then begin
     StringGridGoods.Cells[8,aRow] := OldValue;
     isEdit := True;
     checkErrors;
  end;
end;

procedure TfrmStoreHouses.refreshListOfGoods;
var
  i:Integer;
begin
  FrmMain.DSStoreHousesNomenclatureView.DataSet.Filter := 'storehouseid='+DBGStoreHouses.DataSource.DataSet.FieldByName('id').AsString;
  FrmMain.DSStoreHousesNomenclatureView.DataSet.Filtered := True;
  FrmMain.DSStoreHousesNomenclatureView.DataSet.Refresh;
  StringGridGoods.RowCount := FrmMain.DSStoreHousesNomenclatureView.DataSet.RecordCount+1;
  for i := 1 to FrmMain.DSStoreHousesNomenclatureView.DataSet.RecordCount do begin
    FrmMain.DSStoreHousesNomenclatureView.DataSet.RecNo:=i;
    StringGridGoods.Cells[1,i] := FrmMain.DSStoreHousesNomenclatureView.DataSet.FieldByName('id').AsString;
    StringGridGoods.Cells[2,i] := FrmMain.DSStoreHousesNomenclatureView.DataSet.FieldByName('title').AsString;
    StringGridGoods.Cells[3,i] := FrmMain.DSStoreHousesNomenclatureView.DataSet.FieldByName('quantity').AsString;
    StringGridGoods.Cells[4,i] := FrmMain.DSStoreHousesNomenclatureView.DataSet.FieldByName('quantitymin').AsString;
    StringGridGoods.Cells[5,i] := FrmMain.DSStoreHousesNomenclatureView.DataSet.FieldByName('price').AsString;
    StringGridGoods.Cells[6,i] := CurrToStr(FrmMain.DSStoreHousesNomenclatureView.DataSet.FieldByName('quantity').AsInteger*FrmMain.DSStoreHousesNomenclatureView.DataSet.FieldByName('price').AsCurrency);
    StringGridGoods.Cells[7,i] := FrmMain.DSStoreHousesNomenclatureView.DataSet.FieldByName('nomenclatureid').AsString;
  end;
end;

procedure TfrmStoreHouses.FormShow(Sender: TObject);
begin
  frmMain.ZTStoreHouses.Refresh;
  refreshListOfGoods;
  checkErrors;
  StringGridGoods.Row:=StringGridGoods.RowCount;
end;

procedure TfrmStoreHouses.DBGStoreHousesCellClick(Column: TColumn);
begin
  refreshListOfGoods;
  checkErrors;
end;

procedure TfrmStoreHouses.DBNavigatorBeforeAction(Sender: TObject;
  Button: TDBNavButtonType);
begin
  if Button=nbDelete then begin
    DBNavigator.DataSource.DataSet.Cancel;
  end;
end;

procedure TfrmStoreHouses.btnAddClick(Sender: TObject);
begin
  StringGridGoods.RowCount:=StringGridGoods.RowCount+1;
  StringGridGoods.Row:=StringGridGoods.RowCount;
  isAppend:=True;
  isEdit:=True;
  checkErrors;
end;

procedure TfrmStoreHouses.btnCancelClick(Sender: TObject);
begin
  StringGridGoods.RowCount:=StringGridGoods.RowCount-1;
  isAppend:=False;
  isEdit:=False;
  refreshListOfGoods;
  checkErrors;
end;

procedure TfrmStoreHouses.btnDeleteClick(Sender: TObject);
var
  res:Integer;
begin
  res:=MessageDlg('','Переконайтесь що запис ніде не використовується, Видалити запис?',mtConfirmation,mbYesNo,0);
  if (res=mrYes) then begin
    FrmMain.ZQuery.SQL.Text:='DELETE FROM nomenclatureonstorehouses WHERE id='+StringGridGoods.Cells[1,StringGridGoods.Row];
    FrmMain.ZQuery.ExecSQL;
    FrmMain.DSNomenclature.DataSet.Refresh;
  end;
  refreshListOfGoods;
  checkErrors;
end;

procedure TfrmStoreHouses.btnSaveClick(Sender: TObject);
var
  i:Integer;
  error:Boolean=False;
  quantity:String;
begin
  for i:=1 to StringGridGoods.RowCount-1 do begin
    if (StringGridGoods.Cells[2,i]=EmptyStr) OR (StringGridGoods.Cells[3,i]=EmptyStr) then begin
      error:=True;
    end;
  end;
  if NOT error then begin
    for i:=1 to StringGridGoods.RowCount-1 do begin
      if StringGridGoods.Cells[1,i]=EmptyStr then begin
        if not FrmMain.getIsHaveRecord(DBGStoreHouses.DataSource.DataSet.FieldByName('id').AsString,StringGridGoods.Cells[7,i]) then begin
          frmMain.updateStoreHousesGoodsCount(StringGridGoods.Cells[3,i],DBGStoreHouses.DataSource.DataSet.FieldByName('id').AsString,StringGridGoods.Cells[7,i]);
        end else begin
          ShowMessage('На складі вже є присутня позиція');
        end;
      end else begin
        /////////////////Update count of goods if changed, in nomenclature and storehouse///////////////////
        quantity:='0';
        if ((StringGridGoods.Cells[7,i]<>EmptyStr) AND (StringGridGoods.Cells[3,i]<>StringGridGoods.Cells[8,i]) AND (StringGridGoods.Cells[8,i]<>EmptyStr)) then begin
          if StrToInt(StringGridGoods.Cells[8,i])<StrToInt(StringGridGoods.Cells[3,i]) then begin
            quantity:=IntToStr(StrToInt(StringGridGoods.Cells[3,i])-StrToInt(StringGridGoods.Cells[8,i]));
          end else begin
            quantity:='-'+IntToStr(StrToInt(StringGridGoods.Cells[8,i])-StrToInt(StringGridGoods.Cells[3,i]));
          end;
          if StrToInt(StringGridGoods.Cells[8,i])=StrToInt(StringGridGoods.Cells[3,i]) then begin
            quantity:='0';
          end;
          StringGridGoods.Cells[8,i]:='';
          frmMain.updateStoreHousesGoodsCount(quantity,DBGStoreHouses.DataSource.DataSet.FieldByName('id').AsString,StringGridGoods.Cells[7,i]);
        end;
      end;
    end;
  isAppend:=False;
  isEdit:=False;
  refreshListOfGoods;
  checkErrors;
  end else begin
    ShowMessage('Введіть дані');
  end;

end;

end.

